package hunterxhunter.greedisland.states;

import hunterxhunter.greedisland.GreedIslandGame;
import hunterxhunter.greedisland.data.enums.NenType;
import hunterxhunter.greedisland.data.Ring;
import hunterxhunter.greedisland.entities.text.SelectionEntity;
import hunterxhunter.greedisland.gui.Dialog;
import java.awt.Color;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import framework.eea.StateBasedGame;
import framework.eea.entity.EntityManager;
import framework.eea.event.Component;
import framework.eea.exception.EEAException;
import framework.eea.gui.Graphics;
import framework.eea.gui.JFrameGameContainer;
import framework.eea.interfaces.Action;
import framework.eea.state.GameState;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class MainMenuState extends GameState {

    @Override
    public void init(JFrameGameContainer gc, StateBasedGame sbg) throws EEAException {
        
        EntityManager.getInstance().addEntity(ID, new SelectionEntity(
                null, 
                390, 50, 20, true,
                Color.white, Color.red, 
                new String[] {
                    "Use Nen",
                    "Don't Play"
                }, new Action[] {
                    new Action(){
                        @Override
                        public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                            
                            Ring ring = null;
                            
                            File ringFile = Ring.find();
                            if(ringFile != null) {
                                ring = Ring.loadRing(ringFile);
                            } else {
                                String nick = Dialog.askNickname(gc.getFrame());
                                if(nick != null) {
                                    NenType type = Dialog.askNenType(gc.getFrame());
                                    ring = new Ring(nick, type);
                                    ring.save();
                                }
                            }
                            
                            ((GreedIslandGame)sb).setRing(ring);
                            
                            System.out.println(ring);
                            
                            if(ring != null) {
                                try {
                                    sb.enterState(GameplayState.ID);
                                } catch (EEAException ex) {
                                    Logger.getLogger(MainMenuState.class.getName()).log(Level.SEVERE, null, ex);
                                    sb.quit();
                                }
                            }
                        }
                    },
                    new Action(){
                        @Override
                        public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                            sb.quit();
                        }
                    },
                }));
    }
    
    @Override
    public void render(JFrameGameContainer gc, StateBasedGame sbg, Graphics g) throws EEAException {
        
        int height = gc.getImage("/greedisland/img/joystation.png").getHeight(null);
        g.drawImage("/greedisland/img/joystation.png", 0, GreedIslandGame.getScreenSize().height - height);
    
        g.drawImage("/greedisland/img/greedisland.png", 0, 150);
        
        g.drawImage("/greedisland/img/hunterxhunter.png", 10, 10);
        
        EntityManager.getInstance().renderEntities(gc, sbg, g);
    }
    
    /**
     * ID = 1
     */
    public static final int ID = 1;
    @Override
    public int getID() {
        return ID;
    }

}
